package com.acc.store.contorller;

import com.acc.store.common.Result;
import com.acc.store.entity.Product;
import com.acc.store.service.ProductService;
import com.acc.store.vo.ProductVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/product")
@Slf4j
@Tag(name = "商品操作")
public class ProductController {
    @Autowired
    ProductService productService;

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除商品")
    public Result delete(@PathVariable Long id){
        productService.removeById(id);
        return Result.success(null);
    }
    @PutMapping("/update")
    @Operation(summary = "更新商品")
    public Result update(@RequestBody Product product) {
        productService.updateById(product);
        return Result.success(null);
    }

    @GetMapping("/getbybarcode/{barcode}")
    @Operation(summary = "条码查询")
    public Result getProduct(@PathVariable Long barcode) {
        Product product = productService.getProduct(barcode);
        if (product == null) {
            return Result.error("没有这个商品");
        }
        return Result.success(product);
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "商品回显")
    public Result getById(@PathVariable Long id) {
        ProductVo byId = productService.getVoById(id);
        if(byId==null){
            return Result.error("没有获取到相关的商品信息，请尝试添加商品或重新录入");
        }
        return Result.success(byId);
    }

    @GetMapping("/list")
    @Operation(summary = "商品分页")
    public Result page(@RequestParam Map<String, Object> params) {

        IPage page = productService.getPage(params);

        return Result.success(page);

    }

    @PostMapping("/save")
    @Operation(summary = "商品保存")

    public Result save(@RequestBody Product product) {

        productService.save(product);

        return Result.success("成功");

    }
}
